import { defineStore } from 'pinia'
import type { AddressItem } from '@/types/address'
import { ref } from 'vue'

//创建地址持久化数据
export const useAddressStore = defineStore('address', () => {
  //地址数据信息
  const addressProfile = ref<AddressItem>()

  //修改持久化地址数据
  const changeAddressProfile = (val : AddressItem) => {
    addressProfile.value = val
  }

  // 删除地址数据
  const delAddressProfile = () => {
    addressProfile.value = undefined
  }

  return {
    addressProfile,
    changeAddressProfile,
    delAddressProfile
  }
}, {
  persist: {
    // 由于这是小程序开发，单单persist：true是不够的，要额外配置。参考文档https://prazdevs.github.io/pinia-plugin-persistedstate/zh/guide/config.html#storage中storage
    storage: {
      getItem(key) {
        return uni.getStorageSync(key)
      },
      setItem(key, value) {
        return uni.setStorageSync(key, value)
      }
    }
  },
})
